package com.atguigu.daijia.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/*
@description:时间工具类
@ClassName DateUtils
@author chen
@create 2024-08-22 17:45
@Version 1.0
*/
public class DateUtils {

    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /*
     * @==============History===============<br/>;
     * @Description //   获取当前时间
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static Date getNowDate() {
        return new Date();
    }

    /*
     * @==============History===============<br/>;
     * @Description //   获取当前年份
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static String getYear(){
        return parseDateToStr(YYYY, getNowDate());
    }

    /*
     * @==============History===============<br/>;
     * @Description //   获取年月日
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static String getDate() {
        return parseDateToStr(YYYY_MM_DD,getNowDate());
    }

    /*
     * @==============History===============<br/>;
     * @Description //   获取当前时间的年月日时分秒
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static String getDateTime(){
        return parseDateToStr(YYYY_MM_DD_HH_MM_SS,getNowDate());
    }


    /*
     * @param format     解析的时间格式
     * @param date       解析的时间
     * @==============History===============<br/>;
     * @Description //   解析当前时间
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static String parseDateToStr(String format, Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    /*
     * @param endDate   结束时间
     * @param nowDate   开始时间
     * @==============History===============<br/>;
     * @Description //   计算两个日期的相差时间
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }


    /*
     * @param endDate  结束时间
     * @param nowDate  开始时间
     * @==============History===============<br/>;
     * @Description //   获取两个时间相差的分钟数
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static long timeInterval(Date endDate, Date nowDate) {
        long diff = endDate.getTime() - nowDate.getTime();
        //此处用毫秒值除以分钟再除以毫秒既得两个时间相差的分钟数
        return diff / 60 / 1000;
    }

    /*
     * @param date 
     * @==============History===============<br/>;
     * @Description //   获取当前日期的月天数
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static int getMonthDays(Date date) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date);
        int month = cal1.get(Calendar.MONTH);
        int year = cal1.get(Calendar.YEAR);

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR,year);// 设置年份
        cal.set(Calendar.MONTH, month - 1);  // 设置月份
        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /*
     * @param time 
     * @==============History===============<br/>;
     * @Description //   将string类型数据转时间戳
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static long timeToStampSecond(String time) {
        return timeToStampSecond(time,YYYY_MM_DD_HH_MM_SS);
    }
    
    /*
     * @param time       需要转换的数据类型
     * @param pattern    需要转换日期格式的格式
     * @==============History===============<br/>;
     * @Description //   将string类型数据转时间戳
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static long timeToStampSecond(String time,String pattern) {
        Date d = new Date();
        long timeStamp = 0;
        try {
            SimpleDateFormat sf = new SimpleDateFormat(pattern);
            d = sf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        timeStamp = d.getTime() / 1000;
        return timeStamp;
    }


    /*
     * @param value
     * @==============History===============<br/>;
     * @Description //   将秒数转换为时分秒
     * @Date 2024/8/22
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    public static String formatHMS(long value) {
        String str = "";
        long i = value / (3600 * 24);
        if (i > 0) {
            str = i + "天";
        }
        value %= 3600 * 24;
        i = value / 3600;
        if (i > 0) {
            str += i + "时";
        }
        value %= 3600;
        i = value / 60;
        if (i > 0) {
            str += i + "分";
        }
        value %= 60;
        str += value + "秒";
        return str;
    }


}
